<template>
    <div>
    <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="650" :styles="{top: '100px'}">
        <Form ref="form" :model="form" :label-width="70" :rules="formValidate">
            <FormItem label="关键词" prop="keyword">
                <Input v-model="form.keyword" autocomplete="off"/>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="text" @click="cancelUser">取消</Button>
            <Button type="primary" :loading="submitLoading" @click="handelSubmit">提交</Button>
        </div>
    </Modal>
    </div>
</template>

<script>
    import {addKeyWord,deleteKeyWord, getKeyWordList} from "@/api/index";
    export default {
        name: "keywordDrawer",
        components: {

        },
        data() {
            return {
                type:0,
                accessToken: {},
                errorPass: "",
                deptLoading: false,
                modalTitle: "",
                dataDep: [],
                userRoles: [],
                roleList: [],
                submitLoading: false,
                modalVisible: false,
                form: {
                    id:"",
                    keyword: ""
                },
                formValidate: {
                    keyword: [{required: true, message: "名称不能为空", trigger: "blur"}]
                }
            }
        },
        methods: {
            init() {

            },
            getData() {
                this.loading = true;
            },
            showDrawer(params) {
                this.modalVisible = true;
                if (params.row) {
                    this.type = 1;
                    this.modalTitle = "编辑关键词";
                    this.form=params.row;
                    this.getData();
                } else {
                    this.$refs.form.resetFields();
                    this.modalTitle = "添加关键词";
                    this.type = 0;
                }
            },
            cancelUser() {
                this.modalVisible = false;
            },
            handelSubmit() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.submitLoading = true;
                        if (this.type === 0) {
                            // 添加 避免编辑后传入id等数据 记得删除
                            delete this.form.id;
                            addKeyWord(this.form).then(res => {
                                this.submitLoading = false;
                                if (res.status === true) {
                                    this.$Message.success("添加成功");
                                    this.$parent.getDataList();
                                    this.modalVisible = false;
                                }
                            });
                        } else if(this.type === 1){
                            // 编辑
                            addKeyWord(this.form).then(res => {
                                this.submitLoading = false;
                                if (res.status === true) {
                                    this.$Message.success("修改成功");
                                    this.$parent.getDataList();
                                    this.modalVisible = false;
                                }
                            });
                        }
                    }
                });
            }
        },
        mounted() {

        }
    }
</script>

<style>

</style>